﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Demo
{
    /// <summary>
    /// Interaction logic for FasFriendList.xaml
    /// </summary>
    public partial class FasFriendList : UserControl, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        //此方法在每个属性的set中调用
        //在可选参数propertyName上应用CallerMemberName特性
        //调用此方法时,调用方的信息将替换为可选参数的参数,即在set中调用此方法,propertyName="当前属性的名称"
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        #region DisplayName
        public string DisplayName
        {
            get { return (string)GetValue(DisplayNameProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty DisplayNameProperty =
    DependencyProperty.Register("DisplayName", typeof(string), typeof(FasFriendList), new PropertyMetadata(DisplayNameChangedCallback));
        private static void DisplayNameChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var disname = e.NewValue;
        }
        #endregion

        #region ChildListName
        public string ChildListName
        {
            get { return (string)GetValue(ChildListNameProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty ChildListNameProperty =
    DependencyProperty.Register("ChildListName", typeof(string), typeof(FasFriendList), new PropertyMetadata(ChildListNameChangedCallback));
        private static void ChildListNameChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var disname = e.NewValue;
        }
        #endregion

        #region ChildDisPlayName
        public string ChildDisPlayName
        {
            get { return (string)GetValue(ChildDisPlayNameProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty ChildDisPlayNameProperty =
    DependencyProperty.Register("ChildDisPlayName", typeof(string), typeof(FasFriendList), new PropertyMetadata(ChildDisPlayNameChangedCallback));
        private static void ChildDisPlayNameChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var disname = e.NewValue;
        }
        #endregion

        #region ChildImageName
        public string ChildImageName
        {
            get { return (string)GetValue(ChildImageNameProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty ChildImageNameProperty =
    DependencyProperty.Register("ChildImageName", typeof(string), typeof(FasFriendList), new PropertyMetadata(ChildImageNameChangedCallback));
        private static void ChildImageNameChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var disname = e.NewValue;
        }
        #endregion

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(FasFriendList), new PropertyMetadata(ItemsSourceChangedCallback));

        private static void ItemsSourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int a = 3;
            (d as FasFriendList).FriendGroups?.Clear();
            (d as FasFriendList).FriendGroups = new ObservableCollection<FriendGroup>();
            var list = e.NewValue as IEnumerable;
            foreach (var item in list)
            {
                var group = new FriendGroup();
                group.Name = item.GetType().GetProperties().First(m => m.Name == (d as FasFriendList).DisplayName).GetValue(item).ToString();
                foreach (var childitem in item.GetType().GetProperties().First(m => m.Name == (d as FasFriendList).ChildListName).GetValue(item) as IEnumerable) 
                {
                    var entity = new FriendEntity();
                    entity.Name = childitem.GetType().GetProperties().First(m => m.Name == (d as FasFriendList).ChildDisPlayName).GetValue(childitem).ToString();
                    entity.HeadImage = childitem.GetType().GetProperties().First(m => m.Name == (d as FasFriendList).ChildImageName).GetValue(childitem).ToString();
                    group.Friends.Add(entity);
                }
                (d as FasFriendList).FriendGroups.Add(group);
            }
        }



        private ObservableCollection<FriendGroup> _friendGroups { get; set; }
        public ObservableCollection<FriendGroup> FriendGroups
        {
            get
            {
                return _friendGroups;
            }
            set
            {
                _friendGroups = value;
                NotifyPropertyChanged();
            }
        }


        public FasFriendList()
        {
            InitializeComponent();
            root.DataContext = this;
            int a = 4;
        }
    }
}
